package com.memorywords.controller;

import com.memorywords.pojo.PageResult;
import com.memorywords.pojo.Result;
import com.memorywords.pojo.User;
import com.memorywords.pojo.Word;
import com.memorywords.service.UserService;
import com.memorywords.service.WordService;
import com.memorywords.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@RestController
@RequestMapping("/word")
public class WordController {

    @Autowired
    private WordService wordService;
    @Autowired
    private UserService userService;

    @GetMapping("/getNew")
    public Result<List<Word>> getNew(@RequestHeader("Authorization") String token){
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User loginUser = userService.findByUserName(username);
        int progress = loginUser.getProgress();
        List<Word> list = wordService.getNew(progress);
        return Result.success(list);
    }

    @PostMapping("/mark")
    public Result mark(@RequestHeader("Authorization") String token, int wordId){
        Map<String, Object> map = ThreadLocalUtil.get();
        int userId = (int) map.get("userId");
        wordService.mark(userId, wordId);
        return Result.success();
    }

    @GetMapping("/getOld")
    public Result<List<Word>> getOld(@RequestHeader("Authorization") String token){
        Map<String, Object> map = ThreadLocalUtil.get();
        int userId = (int) map.get("userId");
        return Result.success(wordService.getOld(userId));
    }

    @DeleteMapping("/remove")
    public Result remove(@RequestHeader("Authorization") String token, int wordId){
        Map<String, Object> map = ThreadLocalUtil.get();
        int userId = (int) map.get("userId");
        wordService.remove(userId, wordId);
        return Result.success();
    }

    @GetMapping("/getAll")
    public Result<PageResult> getAll(@RequestHeader("Authorization") String token, int pageNum, int pageSize){
        Map<String, Object> map = ThreadLocalUtil.get();
        int userId = (int) map.get("userId");
        PageResult list = wordService.getAll(userId, pageNum, pageSize);
        return Result.success(list);
    }


}
